<?php
/**
 * @author by bilee.
 * 时间: 16/6/17 上午6:08
 * 帮你跑腿APP管理后台
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class TestController extends AdminbaseController
{
    protected $test_model;
    function _initialize()
    {
        parent::_initialize();
        $this->test_model = D("Common/TestDatabase");
    }
    //题库管理
    public function test(){

        $test_db = M("test_database");
        $count = $test_db->count();
        $page = $this->page($count, 20);

        $this->assign("datas",$test_db->limit($page->firstRow . ',' . $page->listRows)->select());
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

//添加考试题库
    public function addTest()
    {
        if(IS_POST){
            $_POST['testdb_content'] =  I('testdb_content','','trim');
            $_POST['testdb_answer1'] =  I('testdb_answer1','','trim');
            $_POST['testdb_answer2'] =  I('testdb_answer2','','trim');
            $_POST['testdb_answer3'] =  I('testdb_answer3','','trim');
            $_POST['testdb_answer4'] =  I('testdb_answer4','','trim');
            if($this->test_model->create()) {
                $ret = $this->test_model->add();
                if ($ret) {
                    $this->success("新增成功！", U("test"));
                } else {
                    $this->error("新增失败！", U("test"));
                }
            }else{
                $this->error($this->test_model->getError());
            }
        }else{
            $this->display();
        }
    }
    //修改考试题库
    function edittest()
    {
        if (IS_POST) {
            $_POST['testdb_content'] =  I('testdb_content','','trim');
            $_POST['testdb_answer1'] =  I('testdb_answer1','','trim');
            $_POST['testdb_answer2'] =  I('testdb_answer2','','trim');
            $_POST['testdb_answer3'] =  I('testdb_answer3','','trim');
            $_POST['testdb_answer4'] =  I('testdb_answer4','','trim');
            if ($this->test_model->create()) {
                $ret = $this->test_model->save();
                if($ret){
                    $this->success("修改成功！",U("test"));
                }else{
                    $this->error($this->test_model->getError());
                }
            }else{
                $this->error($this->test_model->getError());
            }
        } else {
            $ret = $this->test_model->where(array('testdb_id' => I('test_id')))->find();
            $this->assign('datas', $ret);
            $this->display();
        }
    }
    //考试题库删除
    function deleteTest()
    {
        $ret = $this->test_model->delete(I('testdb_id'));
        if($ret){
            $this->success("删除成功！",U("test"));
        }else{
            $this->error("删除失败");
        }
    }

}